package com.qf.fmall2204.wxpay;

import cn.hutool.core.util.XmlUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpRequest;

import java.util.*;

public class WxDemo {
    public static final String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
    public static final String appkey = "sbNCm1JnevqI36LrEaxFwcaT0hkGxFnC";
    public static final String appid = "wx632c8f211f8122c6";
    public static final String mch_id = "1497984412";

    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<>();
        String nonce = UUID.randomUUID().toString().replaceAll("-", "");
        map.put("nonce_str",nonce); // 长度32个字符的随机字符串，干扰项
        map.put("out_trade_no","202307121920001");
        map.put("total_fee","1");
        map.put("appid","wx632c8f211f8122c6");
        map.put("trade_type","NATIVE"); // 支付类型
        map.put("fee_type","CNY"); // 币种
        map.put("body","testproduct"); // 订单描述信息
        map.put("notify_url","http://47.118.45.73:8080/pay/callback"); // 商户提供的回调地址
        map.put("mch_id","1497984412"); //商户号
        map.put("sign_type","MD5"); // 签名算法

        String sign = generateSignature(map, appkey);
        map.put("sign",sign);

        String xml = XmlUtil.mapToXmlStr(map);
        String result = HttpRequest.post(url).body(xml).execute().body();
        System.out.println(result);

        Map<String, Object> stringObjectMap = XmlUtil.xmlToMap(result);
        System.out.println(stringObjectMap);

    }

    public static String generateSignature(final Map<String, String> data, String key){
        Set<String> keySet = data.keySet();
//        String[] keyArray = keySet.toArray(new String[keySet.size()]);
//        Arrays.sort(keyArray);
        ArrayList<String> list = new ArrayList<>(keySet);
        list.sort(String::compareTo);
        StringBuilder sb = new StringBuilder();
        for (String k : list) {
            if (data.get(k).trim().length() > 0) // 参数值为空，则不参与签名
                sb.append(k).append("=").append(data.get(k).trim()).append("&");
        }
        sb.append("key=").append(key);
        String s = DigestUtil.md5Hex(sb.toString());
        return s.toUpperCase();
    }

    private static void test1() {

        String body = "<xml>\n" +
                "    <appid>wx2421b1c4370ec43b</appid>\n" +
                "    <attach>支付测试</attach>\n" +
                "    <body>JSAPI支付测试</body>\n" +
                "    <mch_id>10000100</mch_id>\n" +
                "    <detail>\n" +
                "        <![CDATA[{ \"goods_detail\":[ { \"goods_id\":\"iphone6s_16G\", \"wxpay_goods_id\":\"1001\", \"goods_name\":\"iPhone6s 16G\", \"quantity\":1, \"price\":528800, \"goods_category\":\"123456\", \"body\":\"苹果手机\" }, { \"goods_id\":\"iphone6s_32G\", \"wxpay_goods_id\":\"1002\", \"goods_name\":\"iPhone6s 32G\", \"quantity\":1, \"price\":608800, \"goods_category\":\"123789\", \"body\":\"苹果手机\" } ] }]]>\n" +
                "    </detail>\n" +
                "    <nonce_str>1add1a30ac87aa2db72f57a2375d8fec</nonce_str>\n" +
                "    <notify_url>https://wxpay.wxutil.com/pub_v2/pay/notify.v2.php</notify_url>\n" +
                "    <openid>oUpF8uMuAJO_M2pxb1Q9zNjWeS6o</openid>\n" +
                "    <out_trade_no>1415659990</out_trade_no>\n" +
                "    <spbill_create_ip>14.23.150.211</spbill_create_ip>\n" +
                "    <total_fee>1</total_fee>\n" +
                "    <trade_type>JSAPI</trade_type>\n" +
                "    <sign>0CB01533B8C1EF103065174F50BCA001</sign>\n" +
                "</xml>";
        String result = HttpRequest.post(url).body(body).execute().body();
        System.out.println(result);
    }
}
